package quests.Q405_PathToACleric;

import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;
import net.sf.l2j.gameserver.model.quest.State;
import net.sf.l2j.gameserver.network.serverpackets.SocialAction;

/**
 * @author alexa026
 */

public class Q405_PathToACleric extends Quest
{
	private static final String qn = "Q405_PathToACleric";
	
	//Items
	private static final int Order1 = 1191;
	private static final int Order2 = 1192;
	private static final int BookOfLionel = 1193;
	private static final int BookOfVivi = 1194;
	private static final int BookOfSimplon = 1195;
	private static final int BookOfPraga = 1196;
	private static final int CertificateOfGallint = 1197;
	private static final int PendantOfMother = 1198;
	private static final int NecklaceOfMother = 1199;
	private static final int LionelCovenant = 1200;
		
	//Rewards
	private static final int MarkOfFaith = 1201;
	
	//NPCs
	private static final int Zigaunt = 30022;
	private static final int Simplon = 30253;
	private static final int Praga = 30333;
	private static final int Lionel = 30408;
	private static final int Gallint = 30017;
	private static final int Vivyan = 30030;
	
	public Q405_PathToACleric(int questId, String name, String descr)
	{
		super(questId, name, descr);
		questItemIds = new int[] { Order1, Order2, BookOfLionel, BookOfVivi, BookOfSimplon, BookOfPraga, CertificateOfGallint, PendantOfMother, NecklaceOfMother, LionelCovenant };
		
		addStartNpc(Zigaunt);
		addTalkId(Zigaunt, Simplon, Praga, Lionel, Gallint, Vivyan);
		
		addKillId(20029);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		
		if (event.equalsIgnoreCase("30022-01.htm"))
		{
			if (player.getClassId().getId() == 0x0a)
			{
				if (player.getLevel() >= 19)
				{
					if (st.getQuestItemsCount(MarkOfFaith) == 1)
					{
						htmltext = "30022-04.htm";
						st.exitQuest(true);
					}
				}
				else
				{
					htmltext = "30022-03.htm";
					st.exitQuest(true);
				}
			}
			else if (player.getClassId().getId() == 0x13)
			{
				htmltext = "30022-02a.htm";
				st.exitQuest(true);
			}
			else
			{
				htmltext = "30022-02.htm";
				st.exitQuest(true);
			}
		}
		else if (event.equalsIgnoreCase("30022-05.htm"))
		{
			st.setState(State.STARTED);
			st.set("cond", "1");
			st.giveItems(Order1, 1);
			st.playSound("ItemSound.quest_accept");
		}		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = getNoQuestMsg(player);
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		int cond = st.getInt("cond");
		switch (st.getState())
		{
			case State.CREATED:
				htmltext = "30022-01.htm";
				break;
				
			case State.STARTED:
				switch (npc.getNpcId())
				{
					case Zigaunt:
						if(cond == 2)
						{
							if (st.getQuestItemsCount(BookOfSimplon) == 3 && st.getQuestItemsCount(BookOfPraga) == 1 && st.getQuestItemsCount(BookOfVivi) == 1)
							{
								htmltext = "30022-08.htm";
								st.takeItems(BookOfSimplon, 3);
								st.takeItems(BookOfPraga, 1);
								st.takeItems(BookOfVivi, 1);
								st.takeItems(Order1, 1);
								st.giveItems(Order2, 1);
								st.set("cond", "3");
							}
							else
								htmltext = "30022-06.htm";	
						}
						else if (cond == 6)
						{
							htmltext = "30022-09.htm";
							st.takeItems(LionelCovenant, 1);
							st.giveItems(MarkOfFaith, 1);
							st.addExpAndSp(3200, 1500);
							player.broadcastPacket(new SocialAction(player, 3));

							st.playSound("ItemSound.quest_finish");
							st.exitQuest(false);
							st.saveGlobalQuestVar("1ClassQuestFinished", "1");
						}
						else
							htmltext = "30022-07.htm";
						break;
					case Simplon:
						if (cond == 1)
						{
							if (st.getQuestItemsCount(BookOfSimplon) == 0)
							{
								htmltext = "30253-01.htm";
								st.giveItems(BookOfSimplon, 3);
								st.playSound("ItemSound.quest_middle");
							}
						else 
							htmltext = "30253-02.htm";
						}
						break;
					case Praga:
						if (cond == 1)
						{
							if (st.getQuestItemsCount(BookOfPraga) == 0 && st.getQuestItemsCount(NecklaceOfMother) == 0)
							{
								htmltext = "30333-01.htm";
								st.giveItems(NecklaceOfMother, 1);
								st.playSound("ItemSound.quest_middle");
							}
							
							else if (st.getQuestItemsCount(BookOfPraga) == 0 && st.getQuestItemsCount(NecklaceOfMother) == 1 && st.getQuestItemsCount(PendantOfMother) == 0)
								htmltext = "30333-02.htm";
							else if (st.getQuestItemsCount(BookOfPraga) == 0 && st.getQuestItemsCount(NecklaceOfMother) == 1 && st.getQuestItemsCount(PendantOfMother) == 1)
							{
								htmltext = "30333-03.htm";
								st.takeItems(NecklaceOfMother, 1);
								st.takeItems(PendantOfMother, 1);
								st.giveItems(BookOfPraga, 1);
								st.set("cond", "2");
							}
							
						}
						else 
							htmltext = "30333-04.htm";
						break;
					case Vivyan:
						if (cond == 1)
						{
							if (st.getQuestItemsCount(BookOfVivi) == 0)
							{
								htmltext = "30030-01.htm";
								st.giveItems(BookOfVivi, 1);
								st.playSound("ItemSound.quest_middle");
							}
							else
								htmltext = "30030-02.htm";
						}
						break;
					case Lionel:
						if (cond == 3)
						{
							htmltext = "30408-01.htm";
							st.giveItems(BookOfLionel, 1);
							st.playSound("ItemSound.quest_middle");
							st.set("cond", "4");	
						}
						else if (st.getQuestItemsCount(BookOfLionel) == 1)
							htmltext = "30408-02.htm";
						else if (cond == 5)
						{
							htmltext = "30408-04.htm";
							st.takeItems(CertificateOfGallint, 1);
							st.giveItems(LionelCovenant, 1);
							st.set("cond", "6");
						}
						break;
					case Gallint:
						if (cond == 4)
						{
							htmltext = "30017-01.htm";
							st.takeItems(BookOfLionel, 1);
							st.giveItems(CertificateOfGallint, 1);
							st.playSound("ItemSound.quest_middle");
							st.set("cond", "5");
						}
						else 
							htmltext = "30017-02.htm";
						
						break;
				}
		}
		return htmltext;
	}
		
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return null;

		if (st.getQuestItemsCount(NecklaceOfMother) == 1)
		{
			if (st.getQuestItemsCount(PendantOfMother) == 0)
			{
					st.giveItems(PendantOfMother, 1);
					st.playSound("ItemSound.quest_middle");
			}
		}
		return null;
	}

	public static void main(String[] args)
	{
		new Q405_PathToACleric(405, qn, "Path to a Cleric");
	}
}